﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(true)]
    public partial class alter_queue : BaseForm
    {
        public alter_queue()
            : base()
        {
        }

        public alter_queue(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbExecuteAs.DataSource = Utility.execute_as(dbContext);
            cbExecuteAs.DisplayMember = "Name";

            cbProcedureName.DataSource = Utility.proceudres(dbContext);
            cbProcedureName.DisplayMember = "Name";

            cbQueue.DataSource = Utility.queues(dbContext);
            cbQueue.DisplayMember = "Name";

        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("USE [" + dbContext.Database + "];");
            sb.AppendLine();
            if (cbQueue.SelectedIndex > 0)
                sb.AppendLine("ALTER QUEUE " + cbQueue.Text + " ");
            else
                sb.AppendLine("ALTER QUEUE [] ");

            sb.AppendLine("WITH");

            if (!cbStatus.Checked) sb.AppendLine("STATUS = OFF,");
            else sb.AppendLine("STATUS = ON,");

            if (cbActivation.Checked)
            {
                if (cbRetention.Checked)
                {
                    sb.AppendLine("RETENTION = ON,");
                }
                else
                {
                    sb.AppendLine("RETENTION = OFF,");
                }

                sb.AppendLine("ACTIVATION (");

                if (cbActivationStatus.Checked)
                {
                    sb.AppendLine("   STATUS = ON, ");
                }
                else
                {
                    sb.AppendLine("   STATUS = OFF, ");
                }

                sb.AppendLine("   PROCEDURE_NAME = " + cbProcedureName.Text + ", ");

                sb.AppendLine("   MAX_QUEUE_READERS = " + tbMaxQueueReaders.Text + ", ");

                if (cbExecuteAs.Text == "SELF" || cbExecuteAs.Text == "OWNER")
                {
                    sb.AppendLine("   EXECUTE AS " + cbExecuteAs.Text);
                }
                else
                {
                    sb.AppendLine("   EXECUTE AS '" + cbExecuteAs.Text + "'");
                }

                if (cbDrop.Checked)
                {
                    sb.AppendLine("   DROP");
                }

                sb.AppendLine(")");
            }
            else
            {
                if (cbRetention.Checked)
                {
                    sb.AppendLine("RETENTION = ON,");
                }
                else
                {
                    sb.AppendLine("RETENTION = OFF,");
                }

                if (cbDrop.Checked)
                {
                    sb.AppendLine("ACTIVATION ( DROP )");
                }
            }

            tbCommand.Text = sb.ToString();
        }

        private void cbStatus_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbRetention_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbActivation_CheckedChanged(object sender, EventArgs e)
        {
            flpActivation.Visible = cbActivation.Checked;
            if (cbActivation.Checked)
            {
                cbDrop.Checked = false;
            }

            Write();
        }

        private void cbActivationStatus_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbProcedureName_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbMaxQueueReaders_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbExecuteAs_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbFileGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbDrop_CheckedChanged(object sender, EventArgs e)
        {
            if (cbDrop.Checked)
            {
                cbActivation.Checked = false;
            }

            Write();
        }

        private void cbQueue_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }
    }
}